package com.cxm.qyyz.widget;

import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.FloatRange;
import androidx.annotation.Nullable;
import androidx.constraintlayout.utils.widget.ImageFilterView;
import androidx.core.content.ContextCompat;

import com.cxm.qyyz.R;
import com.cxm.qyyz.utils.ImageLoader;

public class IconTextView extends LinearLayout {

    private int textPadLeft;
    private int textPadRight;
    private int textPadTop;
    private int textPadBottom;
    private int iconDirection;
    private int iconSize;
    private int iconCorner;
    private boolean iconIsCircle;
    private Drawable iconDrawable;
    private int iconColor;
    private Drawable textBackground;
    private String text;
    private String textFormat;
    private int textSize;
    private int textColor;
    private int drawablePad;
    private ImageFilterView ifvIcon;
    private TextView tv;
    private int maxTranslationX;
    private int maxTranslationY;
    private int offsetX;
    private int offsetY;

    private @interface IconDirection{
        int LEFT = 0;
        int RIGHT = 1;
        int TOP = 2;
        int BOTTOM = 3;
    }

    public IconTextView(Context context, @Nullable @org.jetbrains.annotations.Nullable AttributeSet attrs) {
        super(context, attrs);

        initAttrs(context, attrs);

        initUI();

    }

    /**
     * 设置起点偏移,-1表示原点为控件中心点
     */
    public void setTranslationOffset(int offsetX,int offsetY){
        this.offsetX = offsetX;
        this.offsetY = offsetY;
    }

    public void setTranslationMax(int maxTranslationX, int maxTranslationY){
        this.maxTranslationX = maxTranslationX;
        this.maxTranslationY = maxTranslationY;
    }

    public void setTranslation(@FloatRange(from = -1,to = 1) float TranslationX, @FloatRange(from = -1,to = 1) float TranslationY){
        post(() -> {
            if (offsetX == -1){
                offsetX = -(int) (getWidth() * 0.5f);
            }
            if (offsetY == -1){
                offsetY = -(int) (getHeight() * 0.5f);
            }
            float offsetXDistance = maxTranslationX * TranslationX + offsetX;
            float offsetYDistance = maxTranslationY * TranslationY + offsetY;
            ObjectAnimator translationXAnim = ObjectAnimator.ofFloat(IconTextView.this, View.TRANSLATION_X, 0,offsetXDistance);
            ObjectAnimator translationYAnim = ObjectAnimator.ofFloat(IconTextView.this, View.TRANSLATION_Y, 0,offsetYDistance);
            AnimatorSet as = new AnimatorSet();
            as.playTogether(translationXAnim,translationYAnim);
            as.setDuration(0);
            as.start();
        });
    }

    private void initUI() {
        tv = new TextView(getContext());
        tv.setText(textFormat != null ? String.format(textFormat,text) : text);
        tv.setTextSize(TypedValue.COMPLEX_UNIT_PX,textSize);
        tv.setTextColor(textColor);
        tv.setPadding(textPadLeft,textPadTop,textPadRight,textPadBottom);
        if (textBackground != null){
            tv.setBackground(textBackground);
        }
        this.addView(tv,new LayoutParams(-2,-2));

        ifvIcon = new ImageFilterView(getContext());
        ifvIcon.setAdjustViewBounds(true);
        ifvIcon.setScaleType(ImageView.ScaleType.FIT_CENTER);
        ifvIcon.setImageDrawable(iconDrawable);
        if (iconColor != 0) {
            ifvIcon.setColorFilter(iconColor);
        }

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            if (iconCorner != -1) {
                ifvIcon.setRound(iconCorner);
            }
            if (iconIsCircle) {
                ifvIcon.setRoundPercent(1f);
            }
        }
        LayoutParams iconParam = new LayoutParams(-2, -2);
        if (iconDirection <= 1) {
            this.setOrientation(HORIZONTAL);
        }else {
            this.setOrientation(VERTICAL);
        }
        if (getOrientation() == VERTICAL) {
            if (iconDirection == IconDirection.TOP) {
                iconParam.bottomMargin = drawablePad;
            }else {
                iconParam.topMargin = drawablePad;
            }
        }else {
            if (iconDirection == IconDirection.LEFT) {
                iconParam.rightMargin = drawablePad;
            }else {
                iconParam.leftMargin = drawablePad;
            }
        }
        iconParam.width = iconSize;
        iconParam.height = iconSize;
        if (iconDirection == IconDirection.LEFT || iconDirection == IconDirection.TOP) {
            this.addView(ifvIcon,0, iconParam);
        }else {
            this.addView(ifvIcon, iconParam);
        }
    }

    private void initAttrs(Context context, @Nullable AttributeSet attrs) {
        if (attrs != null) {
            int textDefSize = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, 14, getResources().getDisplayMetrics());
            TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.IconTextView);
            iconSize = ta.getDimensionPixelSize(R.styleable.IconTextView_itv_icon_size, -2);
            iconCorner = ta.getDimensionPixelSize(R.styleable.IconTextView_itv_icon_corner, -1);
            iconIsCircle = ta.getBoolean(R.styleable.IconTextView_itv_icon_is_circle, false);
            iconDrawable = ta.getDrawable(R.styleable.IconTextView_itv_icon_drawable);
            iconColor = ta.getColor(R.styleable.IconTextView_itv_icon_color,0);
            iconDirection = ta.getInt(R.styleable.IconTextView_itv_icon_direction, 0);
            text = ta.getString(R.styleable.IconTextView_itv_text);
            textFormat = ta.getString(R.styleable.IconTextView_itv_text_format);
            textSize = ta.getDimensionPixelSize(R.styleable.IconTextView_itv_text_size, textDefSize);
            textColor = ta.getColor(R.styleable.IconTextView_itv_text_color, Color.parseColor("#333333"));
            textBackground = ta.getDrawable(R.styleable.IconTextView_itv_text_background);
            textPadLeft = ta.getDimensionPixelSize(R.styleable.IconTextView_itv_text_pad_left, 0);
            textPadRight = ta.getDimensionPixelSize(R.styleable.IconTextView_itv_text_pad_right, 0);
            textPadTop = ta.getDimensionPixelSize(R.styleable.IconTextView_itv_text_pad_top, 0);
            textPadBottom = ta.getDimensionPixelSize(R.styleable.IconTextView_itv_text_pad_bottom, 0);
            drawablePad = ta.getDimensionPixelSize(R.styleable.IconTextView_itv_drawable_pad, 0);
            ta.recycle();
        }
    }

    public void setTextFormat(Object text){
        tv.setText(textFormat != null ? String.format(textFormat, text) : String.valueOf(text));
    }


    public void setText(Object text){
        tv.setText(String.valueOf(text));
    }
    public void setIconURL(String URL){
        if (null!=ifvIcon) ImageLoader.load(getContext(),ifvIcon,URL);
    }

    public void setTextColor(int color){
        tv.setTextColor(color);
    }

    public void setTextColorRes(int colorRes){
        int color = ContextCompat.getColor(getContext(), colorRes);
        tv.setTextColor(color);
    }
}
